//
// Created by zhou on 2021/10/18.
//
#include "iostream"
#include <vector>
using namespace std;
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> a(n, vector<int>(n));
        int top = 0, bottom = n - 1, left = 0, right = n - 1;
        int m = 1;
        while (top <= bottom && left <= right) {
            for (int i = left; i <= right; i++) {
                a[top][i] = m;
                m += 1;
            }
            // top+=1;
            for (int i = top + 1; i <= bottom; i++) {
                a[i][right] = m;
                m++;
            }
            // right-=1;
            if (left < right && top < bottom) {


                for (int i = right - 1; i > left; i--) {
                    a[bottom][i] = m;
                    m++;
                }
                // bottom-=1;
                for (int i = bottom; i > top; i--) {
                    a[i][left] = m;
                    m++;
                }
            }
            // left+=1;
            left++;
            bottom--;
            top++;
            right--;

        }
        return a;
    }
};
int main(){
    Solution solution;
    int n=8;
    vector<vector<int>> a(n,vector<int>(n));
    a=solution.generateMatrix(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout<<a[i][j]<<",";
        }
        cout<<endl;

    }
    return 0;

}
